<template>
  <!-- 表单 -->
  <el-form :inline="true" :model="formInline" class="demo-form-inline">
    <el-form-item label="年级:">
      <el-select v-model="formInline.gradeLevel" placeholder="年级">
        <el-option v-for="level in level.list" :key="level.value" :label="level.name" :value="level.value" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">查询</el-button>
    </el-form-item>
  </el-form>

  <!-- 列表 -->
  <el-table :data="tableData" style="width: 100%" border>
    <el-table-column prop="id" label="ID" />
    <el-table-column prop="title" label="标题" />
    <el-table-column label="学级">
      <template #default="scope">
        <span v-for="level in level.list" :key="level.value">
          <span v-if="scope.row.gradeLevel === level.value">{{ level.name }}</span>
        </span>
      </template>
    </el-table-column>
    <el-table-column prop="createUserName" label="发送人" />
    <el-table-column prop="createTime" label="创建时间" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
        <el-button size="small" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>

  <!-- 分页 -->
  <el-pagination layout="total, sizes, prev, pager, next, jumper" :total="total" :page-size="formInline.pageSize"
    :current-page="formInline.pageIndex" :page-sizes="[10, 20, 30, 40]" @size-change="handleSizeChange"
    @current-change="handleCurrentChange" />
</template>

<script lang="ts" setup>
import { useLevelStore } from '@/store/level'
import { onMounted, reactive, ref } from 'vue'

const level = useLevelStore()
// 表单数据
export interface taskListParams {
  gradeLevel: string | number
  pageIndex: number
  pageSize: number
}
const formInline = reactive<taskListParams>({
  gradeLevel: '',
  pageIndex: 1,
  pageSize: 10
})

// 表格数据
import { taskPage, type taskPageResList } from '@/api/task'
const tableData = ref<taskPageResList[]>([])
// 分页相关
const total = ref(0) // 总条数

function getList() {
  taskPage(formInline).then(res => {
    if (res.code == 1) {
      tableData.value = res.response?.list as taskPageResList[]
      total.value = res.response?.total as number
    }
  })
}
onMounted(() => {
  getList()
})

// 查询方法
const onSubmit = () => {
  console.log('submit!', formInline)
  // 根据 formInline.region 进行查询操作
  // 更新 tableData 数据
  getList()
}

// 编辑方法
const handleEdit = (row: any) => {
  console.log('edit', row)
  // 实现编辑逻辑
}

// 删除方法
const handleDelete = (row: any) => {
  console.log('delete', row)
  // 实现删除逻辑
}

// 分页大小改变时触发
const handleSizeChange = (val: number) => {
  console.log(`每页 ${val} 条`)
  formInline.pageSize = val
  // 重新获取数据
}

// 当前页码改变时触发
const handleCurrentChange = (val: number) => {
  console.log(`当前页: ${val}`)
  formInline.pageIndex = val
  // 重新获取数据
}
</script>

<style>
.demo-form-inline .el-input {
  --el-input-width: 220px;
}

.demo-form-inline .el-select {
  --el-select-width: 220px;
}
</style>